package Similitud;

import jcolibri.method.retrieve.NNretrieval.similarity.LocalSimilarityFunction;

// Reimplementamos la clase interval de jcolibri, para definir los intervalos que nos interesan
public class IntervalBool implements LocalSimilarityFunction {

	/** Interval */
	/** Dos tipos de intervalo:
	 * typeInterval: min(v1, v2);
	 * !typeInterval: 30% min(v1, v2);
	 */
	boolean _typeInterval;

	/** Constructor.*/
	public IntervalBool(boolean typeInterval) {
		_typeInterval = typeInterval;
	}

	public double compute(Object o1, Object o2) throws jcolibri.exception.NoApplicableSimilarityFunctionException{
		if ((o1 == null) || (o2 == null))
			return 0;
		if (!(o1 instanceof java.lang.Number))
			throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o1.getClass());
		if (!(o2 instanceof java.lang.Number))
			throw new jcolibri.exception.NoApplicableSimilarityFunctionException(this.getClass(), o2.getClass());


		Number i1 = (Number) o1;
		Number i2 = (Number) o2;
		
		double v1 = i1.doubleValue();
		double v2 = i2.doubleValue();
		double resul;
		
		if (_typeInterval) resul = 1 - ((double) Math.abs(v1 - v2) / Math.min(v1, v2));
		else resul = 1 - ((double) Math.abs(v1 - v2) / (0.3 * Math.min(v1, v2)));
		if (resul > 1 || resul < 0) return 0;
		else return resul;
	}
	
	public boolean isApplicable(Object o1, Object o2)
	{
		if((o1==null)&&(o2==null))
			return true;
		else if(o1==null)
			return o2 instanceof Number;
		else if(o2==null)
			return o1 instanceof Number;
		else
			return (o1 instanceof Number)&&(o2 instanceof Number);
	}

}
